package result

import (
	"com.yy.api/common/logger"
	"encoding/json"
)

type Result struct {
	ResultCode
	Data interface{} `json:"data,omitempty"`
}

func NewJsonResult(rc *ResultCode, data interface{}) []byte {
	r := &Result{
		ResultCode: ResultCode{
			Code: rc.Code,
			Msg:  rc.Msg,
		},
		Data: data,
	}
	return r.covToJson()
}
func (r *Result) covToJson() []byte {
	jsonBytes, err := json.Marshal(r)
	if err != nil {
		logger.Default.Errorf("序列化失败：%v", err)
	}
	return jsonBytes
}

func Success(data interface{}) []byte {
	return NewJsonResult(SUCCESS, data)
}

func Error(rc *ResultCode) []byte {
	return NewJsonResult(rc, nil)
}
